#include "QCurve.h"

QCurve::QCurve(): xMin(0), xMax(0), yMin(0), yMax(0)
{
    this->setID(0);
    this->setPen(QPen(QBrush(QColor(Qt::black)),1.5));
    this->setLimit(541);
}

void QCurve::findMinMax(void)
{
    double tempXMin, tempXMax, tempYMin, tempYMax;
    tempXMin = tempXMax = tempYMin = tempYMax = 0;
    if (!curveData.isEmpty())
    {
        tempXMin=tempXMax=curveData.first().x();
        tempYMin=tempYMax=curveData.first().y();
    }
    for (int i=0; i<curveData.size(); ++i)
    {
        if (tempXMin > curveData[i].x()) tempXMin = curveData[i].x();
        if (curveData[i].x() > tempXMax) tempXMax = curveData[i].x();
        if (tempYMin > curveData[i].y()) tempYMin = curveData[i].y();
        if (curveData[i].y() > tempYMax) tempYMax = curveData[i].y();
    }
    xMin = tempXMin;
    xMax = tempXMax;
    yMin = tempYMin;
    yMax = tempYMax;
}

void QCurve::formatPoints(QPointF zeroPoint)
{
    curveDataFormatted.clear();
    for (int i=0; i<curveData.size(); ++i)
    {
        curveDataFormatted<<QPointF(curveData[i].x()-xMin+zeroPoint.x(),-curveData[i].y()+zeroPoint.y());
    }
}
void QCurve::setID(int _id)
{
    id = _id;
}
int QCurve::getID(void)
{
    return id;
}

void QCurve::addPoint(QPointF point)
{
    if (curveData.size()<curveLimit)
    {
        curveData << point;
    }
    else
    {
        curveData.remove(0);
        curveData << point;
    }
    this->findMinMax();
}
QPolygonF QCurve::getPoints(void)
{
    return curveData;
}
QPolygonF QCurve::getPointsFormatted(void)
{
    return curveDataFormatted;
}
void QCurve::setLimit(int _curveLimit)
{
    curveLimit = _curveLimit;
}

int QCurve::getLimit(void)
{
    return curveLimit;
}

void QCurve::setPen(QPen _curvePen)
{
    curvePen=_curvePen;
}

QPen QCurve::getPen(void)
{
    return curvePen;
}
double QCurve::getYMin(void)
{
    return yMin;
}
double QCurve::getYMax(void)
{
    return yMax;
}
